package com.yxy.controller.admin;

import com.yxy.common.PageResult;
import com.yxy.common.Result;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.pojo.dto.UserDTO;
import com.yxy.pojo.dto.UserPageQueryDTO;
import com.yxy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("/adminUserController")
@RequestMapping(RequestMapperConstant.ADMIN_REQUEST_MAPPER + "user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户信息
     * @param userPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(UserPageQueryDTO userPageQueryDTO){
        log.info("分页查询用户信息：{}", userPageQueryDTO);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 添加用户信息
     * @param userDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody UserDTO userDTO){
        log.info("添加用户信息：{}", userDTO);
        userService.add(userDTO);
        return Result.success("添加成功");
    }

    /**
     * 修改管理员状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/update/{id}/{status}")
    public Result updateStatus(@PathVariable int id, @PathVariable int status){
        log.info("修改管理员状态：{}, {}", id, status);
        userService.updateStatus(id, status);
        return Result.success("");
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public Result batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除用户：{}", ids);
        userService.batchDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改用户信息
     * @param userDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody UserDTO userDTO){
        log.info("修改用户信息：{}", userDTO);
        userService.update(userDTO);
        return Result.success("修改成功");
    }
}
